package com.jstyle.jclife.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jstyle.jclife.R;
import com.jstyle.jclife.model.SosContact;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/6/11.
 */

public class ContactAdapter extends RecyclerViewBaseAdapter<SosContact.DataBean> {


    public ContactAdapter(List<SosContact.DataBean> mDatas) {
        super(mDatas);
    }

    @Override
    protected void bindData(BaseViewHolder holder, final int position) {
        ContactViewHolder contactViewHolder = (ContactViewHolder) holder;
        Context context=((ContactViewHolder) holder).itemView.getContext();
        String remake = context.getResources().getString(R.string.remakename);
        String counturyCode = context.getString(R.string.Area_code);
        String phone = context.getResources().getString(R.string.phone_number);
        if (mDataList.size() >position) {
            SosContact.DataBean sd = mDataList.get(position);
            remake = (String) sd.getRemake();
            String phoneNumber = sd.getPhone();
            counturyCode = phoneNumber.split("-")[0];
            phone = phoneNumber.split("-")[1];
        }

        contactViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(position>=mDataList.size()?null:mDataList.get(position));
            }
        });
        contactViewHolder.tvName.setText(remake);
        contactViewHolder.tvConturyCode.setText(counturyCode);
        contactViewHolder.tvNumber.setText(phone);
    }


    @Override
    protected int getLayoutId(int viewType) {

        return R.layout.item_contact;
    }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder = null;

        View viewOther = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
        baseViewHolder = new ContactViewHolder(viewOther);

        return baseViewHolder;
    }

    public class ContactViewHolder extends BaseViewHolder {

        @BindView(R.id.iv_icon)
        ImageView ivIcon;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_conturyCode)
        TextView tvConturyCode;
        @BindView(R.id.tv_number)
        TextView tvNumber;

        public ContactViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    public int getItemCount() {
        return 3;
    }

    onItemClickListener listener;

    public void setOnContactClickListener(onItemClickListener onItemClickListener) {
        this.listener = onItemClickListener;
    }

    public interface onItemClickListener {
        public void onItemClick(SosContact.DataBean dataBean);
    }

}
